package com.example.easybuy.service;

import com.example.easybuy.dao.CollectionproductsDao;
import com.example.easybuy.pojo.Collectionproducts;
import com.example.easybuy.pojo.Products;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionproductsServiceImpl implements CollectionproductsService{

    @Autowired
    private CollectionproductsDao collectionproductsDao;

    private Logger logger=Logger.getLogger(getClass());

    /*
    添加商品收藏
     */
    @Override
    public String addConllectionProducts(Collectionproducts collectionproducts) {
        logger.debug("CollectionproductsServiceImpl-addConllectionProducts()-collectionproducts:"+collectionproducts);
        int addRow=collectionproductsDao.addConllectionProducts(collectionproducts);
        String success=null;
        if (addRow>0){
           success="添加收藏商品成功";
        }else{
            success="添加收藏商品失败";
        }
        logger.debug("CollectionproductsServiceImpl-addConllectionProducts()-success:"+success);
        return success;
    }

    /*
    根据商品id查询商品信息
     */
    @Override
    public Products getProductsById(String id) {
        logger.debug("CollectionproductsServiceImpl-getProductsById()-id:"+id);
        int productsId=Integer.valueOf(id);
        Products products=collectionproductsDao.getProductsById(productsId);
        logger.debug("CollectionproductsServiceImpl-getProductsById()-products:"+products);
        return products;
    }

    /*
    查询当前商品在不在当前用户的收藏夹里
     */
    @Override
    public Collectionproducts getProductsIsExist(String productsId, int userId) {
        logger.debug("CollectionproductsServiceImpl-getProductsIsExist()-productsId:"+productsId+",userId:"+userId);
        int proId=Integer.valueOf(productsId);
        Collectionproducts collectionproducts=collectionproductsDao.getProductsIsExist(proId,userId);
        logger.debug("CollectionproductsServiceImpl-getProductsIsExist()-collectionproducts:"+collectionproducts);
        return collectionproducts;
    }

    /*
    收藏了几个商品，最多只能收藏6个
     */
    @Override
    public int getCollUserIdCount(int userId) {
        logger.debug("CollectionproductsServiceImpl-getCollUserIdCount()-userId:"+userId);
        int colleCount=collectionproductsDao.getCollUserIdCount(userId);
        logger.debug("CollectionproductsServiceImpl-getCollUserIdCount()-colleCount:"+colleCount);
        return colleCount;
    }

    /*
    查询当前用户所有的收藏商品
     */
    @Override
    public List<Collectionproducts> getColleAll(int userId) {
        logger.debug("CollectionproductsServiceImpl-getColleAll()-userId:"+userId);
        List<Collectionproducts> colleList=collectionproductsDao.getColleAll(userId);
        logger.debug("CollectionproductsServiceImpl-getColleAll()-colleList:"+colleList);
        return colleList;
    }

    /*
    通过商品id和用户id删除收藏商品
     */
    @Override
    public String deleteColleproduct(int userId, String productsId) {
        logger.debug("CollectionproductsServiceImpl-deleteColleproduct()-userId:"+userId+",productsId:"+productsId);
        int proId=Integer.valueOf(productsId);
        int deletRow=collectionproductsDao.deleteColleproduct(userId,proId);
        String success=null;
        if (deletRow>0){
            success="商品已删除";
        }else {
            success="商品删除失败";
        }
        logger.debug("CollectionproductsServiceImpl-deleteColleproduct()-success:"+success);
        return success;
    }

    /*
    清空所有收藏商品
     */
    @Override
    public String clearAllColle(int userId) {
        logger.debug("CollectionproductsServiceImpl-clearAllColle()-userId:"+userId);
        int clreaRow=collectionproductsDao.clearAllColle(userId);
        String success=null;
        if (clreaRow>0){
            success="清空成功";
        }else {
            success="清空失败";
        }
        logger.debug("CollectionproductsServiceImpl-clearAllColle()-success:"+success);
        return success;
    }
}
